package com.baike.controller;


import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.TbDiscount;
import com.baike.mapper.TbDiscountMapper;
import com.baike.service.impl.TbDiscountServiceImpl;
import com.baike.util.IdWorker;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lbs
 * @since 2021-11-01
 */
@RestController
@RequestMapping("discountController")
@Api("拼团课程数据管理实例")
public class TbDiscountController {
    @Autowired
    private TbDiscountServiceImpl discountService;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private TbDiscountMapper discountMapper;

    @PostMapping
    @ApiOperation("拼团课程申请提交")
    @ApiImplicitParam(value = "拼团课程实体类提交",name = "discount",required = true,paramType = "body")
    public Result postDiscount(@RequestBody TbDiscount discount){
        // 非空判断
        if(Objects.nonNull(discount)){
            discount.setDiscountId(idWorker.nextId());

            System.out.println(discount.toString());
            int insert = discountMapper.insert(discount);
            if(insert > 0){
                return Result.build(ConstantEnum.SUCCESS);
            }else {
                return Result.build(ConstantEnum.ERROR);
            }

        }
        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);
    }

    @DeleteMapping
    @ApiOperation("拼团课程删除")
    @ApiImplicitParam(value = "课程id数组",name = "ids",required = true,paramType = "query")
    public Result delDiscount(@RequestParam("ids")Long[] ids){
        // 非空判断
        if(Objects.nonNull(ids)){
            boolean b = discountService.removeByIds(Arrays.asList(ids));
            if(b){
                return Result.build(ConstantEnum.SUCCESS);
            }else {
                return Result.build(ConstantEnum.ERROR);
            }
        }
        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);
    }

    @PutMapping
    @ApiOperation("拼团课程资料修改")
    @ApiImplicitParam(value = "拼团课程实体类提交",name = "discount",required = true,paramType = "body")
    public Result putDiscount(@RequestBody TbDiscount discount){
        // 非空判断
        if(Objects.nonNull(discount) && Objects.nonNull(discount.getDiscountId())){
            boolean b = discountService.updateById(discount);
            if(b){
                return Result.build(ConstantEnum.SUCCESS);
            }
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);
    }

    @PostMapping("findByWrapper")
    @ApiOperation("根据条件查询拼团课程")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页数",name = "pageNumber",required = false, paramType = "query"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",required = false, paramType = "query"),
            @ApiImplicitParam(value = "课程Id",name = "id",required = false, paramType = "query"),
            @ApiImplicitParam(value = "名称模糊关键字",name = "likeName",required = false, paramType = "query"),
            @ApiImplicitParam(value = "状态 0待审核 1 通过 2 驳回 3结束",name = "status",required = false, paramType = "query"),
            @ApiImplicitParam(value = "一级分类",name = "categories1",required = false, paramType = "query"),
            @ApiImplicitParam(value = "二级分类",name = "categories2",required = false, paramType = "query"),
            @ApiImplicitParam(value = "教师Id",name = "teacherId",required = false, paramType = "query"),
            @ApiImplicitParam(value = "开始时间",name = "startTime",required = false, paramType = "query"),
            @ApiImplicitParam(value = "截至时间",name = "endTime",required = false, paramType = "query")
    })
    public Result findByWrapper(@RequestParam(value = "pageNumber",defaultValue = "0",required = false)Integer pageNumber,
                                @RequestParam(value = "pageSize",defaultValue = "10",required = false)Integer pageSize,
                                @RequestParam(value = "id",required = false)Long id,
                                @RequestParam(value = "likeName",required = false)String likeName,
                                @RequestParam(value = "status",required = false)Integer status,
                                @RequestParam(value = "categories1",required = false)Integer categories1,
                                @RequestParam(value = "categories2",required = false)Integer categories2,
                                @RequestParam(value = "teacherId",required = false)Integer teacherId,
                                @RequestParam(value = "startTime",required = false) Date startTime,
                                @RequestParam(value = "endTime",required = false)Date endTime
                                ){
        Page<TbDiscount> page = new Page<>(pageNumber,pageSize); // 如果不要总记录时 在这里写个 false

        LambdaQueryWrapper<TbDiscount> wrapper = Wrappers.<TbDiscount>lambdaQuery().eq(Objects.nonNull(id), TbDiscount::getDiscountId, id)
                .like(StringUtils.isNotEmpty(likeName),TbDiscount::getDiscountLessonName, likeName)
                .eq(Objects.nonNull(status), TbDiscount::getStatus, status)
                .eq(Objects.nonNull(categories1), TbDiscount::getDiscountCategories1, categories1)
                .eq(Objects.nonNull(categories2), TbDiscount::getDiscountCategories2, categories2)
                .eq(Objects.nonNull(teacherId), TbDiscount::getTeacherId, teacherId)
                .gt(Objects.nonNull(startTime),TbDiscount::getDiscountStartTime,startTime)
                .lt(Objects.nonNull(endTime),TbDiscount::getDiscountEndTime,endTime);
        IPage<TbDiscount> iPage = discountService.page(page, wrapper);
        if(Objects.nonNull(iPage)){
            List<TbDiscount> discountList = iPage.getRecords();
            if(discountList.size() > 0){
                return Result.build(ConstantEnum.SUCCESS).setResult(discountList);
            }else {
                return Result.build(ConstantEnum.NULL_DATA);
            }
        }
        return Result.build(ConstantEnum.NULL_DATA);
    }

    @PutMapping("updateStatus")
    @ApiOperation("根据拼团课程id修改拼团课程状态")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "课程Id",name = "id",required = true,paramType = "query"),
            @ApiImplicitParam(value = "状态码 0待审核 1 通过 2 驳回 3结束",name = "status",required = true,paramType = "query")
    })
    public Result updateStatus(@RequestParam("id")Long id,@RequestParam("status")Integer status){
        // 非空判断
        if(Objects.nonNull(id) && Objects.nonNull(status)){
            UpdateWrapper<TbDiscount> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("discount_id",id).set("status",status);//第一个参数都是数据库列名，不是实体属性名
            boolean b = discountService.update(updateWrapper);
            if(b){

                return Result.build(ConstantEnum.SUCCESS);
            }
            return Result.build(ConstantEnum.ERROR);
        }
        return Result.build(ConstantEnum.NULL_DATA_PARAMETER);
    }

}
